package com.rwy.web.controller;

import java.util.Map;
import java.util.function.Predicate;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.rwy.dto.StatisticDeviceDTO;
import com.rwy.dto.StatisticsSummaryDTO;
import com.rwy.service.StatisticsSummaryService;
import com.rwy.service.UserManageService;
import com.rwy.util.CommonController;
import com.rwy.util.JsonObject;
import com.rwy.util.ObjectUtil;
import com.rwy.util.PageInfo;

@RestController
@RequestMapping("/statistic")
public class StatisticSummaryController extends CommonController {

	@Autowired
	StatisticsSummaryService summaryService;
	@Autowired
	UserManageService userService;
	
	@RequestMapping(value = "/summary", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
	@ResponseBody
	public JsonObject getSummary(HttpServletRequest req) throws Exception  {
		JsonObject json = JsonObject.getInstance();
		StatisticsSummaryDTO data = summaryService.getStatisticSummary(StatisticsSummaryDTO.builder().partnerIDS(getUser(req).getPartnerIds()).businessPartnerID(getUser(req).getBusinessPartnerID()).dateType(StatisticsSummaryDTO.date_type_day).build().buildEndTime().buildStartTime());
		data.setShowEarnings(isShowEarnings);
		json.setData(data);
		setJsonMesasge(json, true);
		return json;
	}

	@RequestMapping(value = "/chart/{type}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
	public JsonObject getChart(@PathVariable("type") Integer type, @RequestParam Integer dateType, HttpServletRequest req) throws Exception  {
		JsonObject json = JsonObject.getInstance();
		boolean pass = false;
		Predicate<Integer> pred = (a) -> a > 0 && a <= 9;
		if (pred.test(type)) {
			json.setData(summaryService.getPrintKindStatistic(StatisticsSummaryDTO.builder().partnerIDS(getUser(req).getPartnerIds()).businessPartnerID(getUser(req).getBusinessPartnerID()).kindType(type).dateType(dateType).build().buildStartTime().buildEndTime()));
			pass = true;
		}
		setJsonMesasge(json, pass);
		return json;
	}

	//基于设备统计改为基于门店统计
	@RequestMapping(value = "/device", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
	public JsonObject getSaleNetworkStatistic(Integer page, Integer pageSize, HttpServletRequest req) throws Exception  {
		JsonObject json = JsonObject.getInstance();
		Map<String, String> param = getCurrentMonthInterval();
		PageInfo<StatisticDeviceDTO> data = summaryService.getSaleNetworkStatisticList(
				StatisticDeviceDTO.builder().partnerIDS(getUser(req).getPartnerIds()).businessPartnerID(getUser(req).getBusinessPartnerID()).startTime(param.get(begining_of_month_key) + " 00:00:00").endTime(param.get(current_date_key) + " 23:59:59").build().page(page).pageSize(pageSize));
		json.setData(data);
		setJsonMesasge(json, true);
		return json;
	}
	
	
	@Deprecated
	//@RequestMapping(value = "/device", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
	public JsonObject getPrintRecordStatisticByTerminal(Integer page, Integer pageSize, HttpServletRequest req) throws Exception  {
		JsonObject json = JsonObject.getInstance();
		Map<String, String> param = getCurrentMonthInterval();
		PageInfo<StatisticDeviceDTO> data = summaryService.getPrintRecordStatisticByTerminal(
				StatisticDeviceDTO.builder().partnerIDS(getUser(req).getPartnerIds()).businessPartnerID(getUser(req).getBusinessPartnerID()).startTime(param.get(begining_of_month_key) + " 00:00:00").endTime(param.get(current_date_key) + " 23:59:59").build().page(page).pageSize(pageSize));
		json.setData(data);
		setJsonMesasge(json, true);
		return json;
	}

	
	
	@Deprecated
	//@RequestMapping(value = "/device/{id}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
	public JsonObject getDeviceRecordDetail(@PathVariable("id") Integer id, HttpServletRequest req) throws Exception  {
		JsonObject json = JsonObject.getInstance();
		Map<String, String> param = getCurrentMonthInterval();
		json.setData(summaryService.getDeviceRecordDetail(
				StatisticDeviceDTO.builder().partnerIDS(getUser(req).getPartnerIds()).businessPartnerID(getUser(req).getBusinessPartnerID()).startTime(param.get(begining_of_month_key) + " 00:00:00").endTime(param.get(current_date_key) + " 23:59:59").termimalID(id).build()));
		setJsonMesasge(json, true);
		return json;
	}

	@RequestMapping(value = "/device/{id}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
	public JsonObject getSalesNetworkRecordDetail(@PathVariable("id") Integer id, HttpServletRequest req) throws Exception  {
		JsonObject json = JsonObject.getInstance();
		Map<String, String> param = getCurrentMonthInterval();
		json.setData(summaryService.getSalesNetworkRecordDetail(
				StatisticDeviceDTO.builder().partnerIDS(getUser(req).getPartnerIds()).businessPartnerID(getUser(req).getBusinessPartnerID()).startTime(param.get(begining_of_month_key) + " 00:00:00").endTime(param.get(current_date_key) + " 23:59:59").termimalID(id).build()));
		setJsonMesasge(json, true);
		return json;
	}
	
	@Deprecated
	//@RequestMapping(value = "/device/{id}/{type}/{datetype}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
	public JsonObject getDeviceRecordChart(HttpServletRequest req, @PathVariable("id") Integer terminalID, @PathVariable("type") Integer type, @PathVariable("datetype") Integer dateType) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = false;
		Predicate<Integer> typePre = (t) -> !ObjectUtil.isEmpty(StatisticsSummaryDTO.printKindMap.get(t));
		if (typePre.test(type)) {
			json.setData(summaryService.getDeviceRecordChartByKindType(StatisticDeviceDTO.builder().partnerIDS(getUser(req).getPartnerIds()).businessPartnerID(getUser(req).getBusinessPartnerID()).termimalID(terminalID).type(type).dateType(dateType).build().buildEndTime().buildStartTime()));
			pass = true;
			setJsonMesasge(json, pass);
		}
		return json;
	}
	
	@RequestMapping(value = "/device/{id}/{type}/{datetype}", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
	public JsonObject getSalesNetworkRecordChart(HttpServletRequest req, @PathVariable("id") Integer terminalID, @PathVariable("type") Integer type, @PathVariable("datetype") Integer dateType) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = false;
		Predicate<Integer> typePre = (t) -> !ObjectUtil.isEmpty(StatisticsSummaryDTO.printKindMap.get(t));
		if (typePre.test(type)) {
			json.setData(summaryService.getSalesNetworkRecordChartByKindType(StatisticDeviceDTO.builder().partnerIDS(getUser(req).getPartnerIds()).businessPartnerID(getUser(req).getBusinessPartnerID()).termimalID(terminalID).type(type).dateType(dateType).build().buildEndTime().buildStartTime()));
			pass = true;
			setJsonMesasge(json, pass);
		}
		return json;
	}
}
